using System;
using System.Reflection;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Autofac;
using Autofac.Integration.Web;
using Autofac.Integration.Web.Mvc;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using log4net;
using log4net.Config;
using NHibernate;
using YourCuppa.Services;

namespace YourCuppa.Web
{
    //// Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    //// visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : HttpApplication
    {
        /// <summary>
        ///   The logger for this web site to use.
        /// </summary>
        private static readonly ILog logger = LogManager.GetLogger("YourCuppa.Web");

        private static IContainerProvider _containerProvider;
        private static ISessionFactory _sessionFactory;
        public static ILog Logger
        {
            get { return logger; }
        }

        public IContainerProvider ContainerProvider
        {
            get { return _containerProvider; }
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default",
                "{controller}/{action}/{id}",
                new {controller = "Home", action = "Index", id = string.Empty});


            routes.MapRoute(
                "OpenIdDiscover",
                "Auth/Discover");
        }

        protected void Application_Start()
        {
            XmlConfigurator.Configure();
            Logger.Info("Web application starting...");

            ConfigureContainer();


            RegisterRoutes(RouteTable.Routes);
        }

        private void ConfigureContainer()
        {
            var builder = new ContainerBuilder();
            builder.RegisterControllers(Assembly.GetExecutingAssembly());
            builder.RegisterType<ShopService>().As<IShopService>();













            _containerProvider = new ContainerProvider(builder.Build());
            ControllerBuilder.Current.SetControllerFactory(new AutofacControllerFactory(ContainerProvider));
        }



        protected void Application_BeginRequest()
        {
            //var newSession = _sessionFactory.OpenSession();
            //NHibernate.Context.CurrentSessionContext.Bind(newSession);
        }

        protected void Application_Error(object sender, EventArgs e)
        {
            Logger.Error(
                "An unhandled exception occurred in ASP.NET processing for page " + HttpContext.Current.Request.Path,
                Server.GetLastError());
        }

        protected void Application_End(object sender, EventArgs e)
        {
            Logger.Info("Web application shutting down...");

            // this would be automatic, but in partial trust scenarios it is not.
            LogManager.Shutdown();
        }
    }
}